﻿using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Linq;
using System.ServiceModel.Web;
using System.Web;
using System.Security;
using System.Diagnostics;
using System.Linq.Expressions;
using RentAWorker.Business.WebHost.Code;

namespace RentAWorker.Business.WebHost
{
    public class RefDataService : DataService<RentAWorker.Business.WebHost.DataService.BigDays2009EntitiesSimple>
    {
        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(IDataServiceConfiguration config)
        {
           try
            {
                config.SetEntitySetAccessRule("Location", EntitySetRights.All);
                config.SetEntitySetAccessRule("MachineType", EntitySetRights.All);
                config.SetEntitySetAccessRule("RentalServiceState", EntitySetRights.All);
                config.SetEntitySetAccessRule("Resource", EntitySetRights.AllRead);
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry("Application",
                    "Rent-A-Worker Data services error occured: " + ex.Message, EventLogEntryType.Error);
                throw ex;
            }
        }

        [QueryInterceptor("Resource")]
        public Expression<Func<DataService.Resource, bool>> OnQueryResource()
        {
            // Get the roles from the custom XML-File
            bool IsInRole = RentAWorkerGroupHelper.IsCurrentUserInRole("ResourceManager");
            return r => IsInRole;
        }

        [ChangeInterceptor("Location")]
        public void OnChangeLocation(DataService.Location location, UpdateOperations operation)
        {
            ValidateRequest();
        }

        [ChangeInterceptor("MachineType")]
        public void OnChangeLocation(DataService.MachineType machineType, UpdateOperations operation)
        {
            ValidateRequest();
        }

        [ChangeInterceptor("RentalServiceState")]
        public void OnChangeLocation(DataService.RentalServiceState location, UpdateOperations operation)
        {
            ValidateRequest();
        }

        protected override void OnStartProcessingRequest(ProcessRequestArgs args)
        {
            if (!RentAWorkerGroupHelper.IsCurrentUserInRole("FrontOffice"))
                throw new SecurityException("Only front-office members may manage these entities! If using Vista, run your client as Admin!");
            
            base.OnStartProcessingRequest(args);

        }

        protected override void HandleException(HandleExceptionArgs args)
        {
            EventLog.WriteEntry("Application",
                                "Rent-A-Worker Data Service Error: " + args.Exception.Message,
                                EventLogEntryType.Error);
            base.HandleException(args);
        }

        private static void ValidateRequest()
        {
            if (!RentAWorkerGroupHelper.IsCurrentUserInRole("ResourceManager"))
            {
                throw new SecurityException("Only resource managers may change reference data!");
            }
        }
    }
}
